$.Test.Functional.extend('Tests.EntriesControllerTest',{
    test_exists: function(){
        this.driver.exists('.entry', this.timeoutCallback('ready', 5000));
    },
    convert : function(id){
        var r = /[#;\&,\.\+\*~':"!\^\$\[\]\(\)=>\|\/%]/g
        return "."+("entry_"+ encodeURIComponent( id )  ).replace(r, function(s){return "\\\\"+s});   
    },
    ready : function(){
        this.assert(true)
    },
    selectContext : function(el, context, callback){
        this.driver.contextmenu(el)
        this.timeout('contextUp', 100, context, callback)
    },
    contextUp : function(context, callback){
        this.driver.click(context)
        this.timeout('afterSelect',50, callback)
    },
    afterSelect : function(callback){
        callback()
    },
    /**
     * Creates a folder with the path, when ready, calls back f
     * @param {Object} path
     * @param {Object} f
     */
    createFile : function(path, f){
        var file = path.replace(/\/[^\/]+$/,"") || "/"
        this.selectContext(this.convert(file), 
                           "#contextmenu .new_file", 
                           this.timeoutCallback('waitFileInput', path, f))
    },
        waitFileInput : function(path, f){
            this.driver.exists("input.newEntry",this.timeoutCallback('writeFileName', path, f));
        },
        writeFileName : function(path, f){
            this.driver.type("input.newEntry",path.match(/[^\/]+$/)[0]+"\n");
            this.driver.exists(this.convert(path), this.timeoutCallback('fileMade', f, path))
        },
        fileMade : function(f, path){
            f();
        },
    /**
     * Creates a folder with the path, when ready, calls back f
     * @param {Object} path
     * @param {Object} f
     */
    createFolder : function(path, f){
        var folder = path.replace(/\/[^\/]+$/,"") || "/"
        this.selectContext(this.convert(folder), "#contextmenu .new_folder", this.timeoutCallback('waitFolderInput', path, f))
    },
        waitFolderInput : function(path, f){
            this.driver.exists("input.newEntry",this.timeoutCallback('writeFolderName', path, f));
        },
        writeFolderName : function(path, f){
            this.driver.type("input.newEntry",path.match(/[^\/]+$/)[0]+"\n");
            this.driver.exists(this.convert(path), this.timeoutCallback('folderMade', f, path))
        },
        folderMade : function(f, path){
            f();
        },
    /**
     * Deletes a folder with a path
     * @param {Object} path
     * @param {Object} f
     */
    deleteFolder : function(path, f){
        this.selectContext(this.convert(path), 
                           "#contextmenu .delete", 
                           this.timeoutCallback('fileDeleted', path, f))
    },
        fileDeleted : function(path, f){
            this.driver.missing(this.convert(path), f)
        },
    
    test_createAndDeleteFile : function(){
        this.createFile("/abc.txt", this.timeoutCallback('deleteABCFile'))
    },
        deleteABCFile : function(){
            this.deleteFolder("/abc.txt", this.timeoutCallback('fileABCDeleted') )
        },
        fileABCDeleted : function(){
            this.assert(true)
        },
        
    
    
    test_createAndDeleteFolder : function(){
        this.createFolder("/abc", this.timeoutCallback('deleteABCFolder'))
    },
        deleteABCFolder : function(){
            this.assert(true)
            this.deleteFolder("/abc", this.timeoutCallback('folderABCDeleted'))
        },
        folderABCDeleted : function(){
            this.assert(true)
        },
    test_rename : function(){
        this.createFile("/rename.this", this.timeoutCallback('renameThis'))
    },
        renameThis : function(){
            this.selectContext(this.convert("/rename.this"), 
                  "#contextmenu .rename", 
                  this.timeoutCallback('typeNewName'))
        },
        typeNewName : function(){
            this.driver.type(".renameEntry","\brename.txt\n")
            this.driver.exists(this.convert("/rename.txt"), this.timeoutCallback('renamed'))
        },
        renamed : function(){
            this.deleteFolder("/rename.txt", this.timeoutCallback('removedRenamed'))
        },
        removedRenamed : function(){
            
        },
        
    test_createNestedFolders : function(){
        this.createFolder("/first", this.timeoutCallback('createNest'))
    },
        createNest : function(){
            this.createFolder("/first/second", this.timeoutCallback('createNest2'))
        },
        createNest2 : function(){
            this.createFile("/first/second.txt", this.timeoutCallback('moveFile'))
        },
        moveFile : function(){
            this.driver.dragAndDrop( this.convert("/first/second.txt"),  this.convert("/first/second") )
            this.driver.exists(this.convert("/first/second/second.txt"), this.timeoutCallback('removeFiles'))
        },
        removeFiles : function(){
            this.deleteFolder("/first/second/second.txt", this.timeoutCallback('secondFileRemoved'))
        },
        secondFileRemoved : function(){
            this.deleteFolder("/first/second", this.timeoutCallback('secondFolderRemoved'))
        },
        secondFolderRemoved : function(){
            this.deleteFolder("/first", this.timeoutCallback('firstRemoved'))
        },
        firstRemoved : function(){
            this.assert(true);
        }
    
});